import {SnowflakeGenerator} from "./SnowflakeGenerator";
import {TimestampGenerator} from "./TimestampGenerator";
import {UuidGenerator} from "./UuidGenerator";
import {IdGenerationStrategy, IdGenerator, SQLiteDataType} from "../../../types";

/**
 * 根据策略获取ID生成器
 * @param strategy ID生成策略
 * @param customGenerator 自定义生成器 (仅当strategy为CUSTOM时使用)
 * @returns ID生成器实例
 */
export function getIdGenerator(
  strategy: IdGenerationStrategy,
  customGenerator?: IdGenerator,
  options?: { nodeId?: number }
): IdGenerator {
  switch (strategy) {
    case IdGenerationStrategy.UUID:
      return new UuidGenerator();
    case IdGenerationStrategy.TIMESTAMP:
      return new TimestampGenerator();
    case IdGenerationStrategy.SNOWFLAKE:
      return SnowflakeGenerator.getInstance(options?.nodeId || 1);
    case IdGenerationStrategy.CUSTOM:
      if (!customGenerator) {
        throw new Error('使用CUSTOM策略时必须提供自定义生成器');
      }
      return customGenerator;
    case IdGenerationStrategy.AUTO_INCREMENT:
    default:
      // AUTO_INCREMENT由SQLite处理，无需生成器
      return {
        generate: () => {
          throw new Error('AUTO_INCREMENT策略由SQLite处理，不应调用此方法');
        },
        fieldType(): SQLiteDataType {
          return 'INTEGER';
        }
      };
  }
}
